package com.rkc.controller;

import com.rkc.request.CartItemRequest;
import com.rkc.service.CartService;
import com.rkc.utils.JsonData;
import com.rkc.vo.CartItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author `RKC`
 * @date 2021/10/30 15:14
 */
@Api("购物车模块")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("添加到购物车")
    @PostMapping("add")
    public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
        cartService.addToCart(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("clear")
    public JsonData clearCart() {
        cartService.clear();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("my_cart")
    public JsonData findMyCart() {
        return JsonData.buildSuccess(cartService.findMyCart());
    }

    @ApiOperation("删除购物项")
    @DeleteMapping("delete/{product_id}")
    public JsonData deleteItem(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") long productId) {
        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }

    @ApiOperation("删除购物列表")
    @PostMapping("deletions")
    public JsonData deleteItemList(@ApiParam(value = "商品列表", required = true) @RequestBody Set<Long> productIdSet) {
        for (Long productId : productIdSet) {
            cartService.deleteItem(productId);
        }
        return JsonData.buildSuccess();
    }

    @ApiOperation("修改购物项数量")
    @PostMapping("update_item")
    public JsonData updateItemCount(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
        cartService.updateItemCount(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("获取对应订单的商品信息")
    @PostMapping("confirm_order_cart_item")
    public JsonData confirmOrderCartItem(@ApiParam("商品id列表") @RequestBody Set<Long> productIdSet) {
        List<CartItemVO> cartItemVOList = cartService.confirmOrderCartItem(productIdSet);
        return JsonData.buildSuccess(cartItemVOList);
    }
}
